
#include <stdio.h>

#ifndef LOGGER_H
#define	LOGGER_H

#define DEBUG(format, ...) logger.log(DEBUG, __PRETTY_FUNCTION__, format, ## __VA_ARGS__)
#define INFO(format, ...) logger.log(INFO, __PRETTY_FUNCTION__, format, ## __VA_ARGS__)
#define WARNING(format, ...) logger.log(WARNING, __PRETTY_FUNCTION__, format, ## __VA_ARGS__)

/**
 * @brief It is used to define the level of the logger.
 * 
 */
enum LogType {
    DEBUG = 2, INFO = 1, WARNING = 0
};

/**
 * @brief It is used to register messages of error, information and warning.
 * @author Jorge Lopez
 * @date March 2013
 */
class Logger {
public:
    /**
     * @brief Builds a Logger class.
     */
    Logger();

    /**
     * @brief Builds a Logger class with a level control.
     * @param levelControl
     */
    Logger(LogType levelControl);

    /**
     * @brief Builds a Logger class with a level control.
     * @param levelControl
     * @param levelControl
     */
    Logger(LogType levelControl,const char* name);
    /**
     * @brief It free up the resources that the class uses.
     */
    ~Logger();

    /**
     * @brief It is used to register messages.
     * 
     * @param type Type of message.
     * @param format Format that the printf uses.
     * @param ... args.
     */
    void log(LogType type, const char* nameFuntion , const char* format, ...);

    /**
     * @brief Sets the level control.
     * @param type new level control.
     */
    void setLevelControl(LogType type);
private:
    void createFile(const char* name);
    /**
     * @brief Gets the current date according to the format.
     * @param format Format used in strftime function.
     * @param buffer where it is copied the date
     * @param size size of the buffer
     */
    void getDate(const char* format, char* buffer, unsigned int size);

    /**
     * @brief Parse a log type to characters.
     * 
     * @param type Log 
     * @return 
     */
    const char* parseToChars(LogType type);
    
    void buildLog();
private:
    /**
     * @brief File where one writes the logs.
     */
    FILE* logFile_;

    /**
     * @brief It is used to control the logs.
     */
    LogType levelControl_;
};


extern Logger logger;
#endif	/* LOGGER_H */

